import addSelButton from "@/components/diy-form/add-sel-button.vue";
import addSelBg from "@/components/diy-form/add-sel-bg.vue";
import shareWechat from '@/components/share/share-wechat.vue';
import MxDatePicker from "@/components/mx-datepicker/mx-datepicker.vue";
import detailOpelist from '@/components/modal/detail-opelist.vue';
export default {
	components:{
		addSelBg,
		addSelButton,
		shareWechat,
		MxDatePicker,
		detailOpelist
	},
	data() {
		return {
			shareInfo:{},
			info:{},
			s_id:0,
			loginStatus:false,
			buttonInfo:{
				show:false,
				bottonStatus:true,
				buttonText:'立即打卡'
			},
			mxDatePicker:{
				showPicker: false,
				type: '',
				joinListTime: '',
				userListTime:'',
				bukaTime:'',
				key:'',
			},
			joinList:[],
			joinListEmpty:'',
			joinListOrderIndex:0,
			joinListOrder:[
				{
					text:'时间降序 ↓',
					order:'desc'
				},
				{
					text:'时间正序 ↑',
					order:'asc'
				}
			],
			userListData:{
				successCount:0,
				list:[],
				dateIndex:0,
				dateList:['今天','昨天','选择日期'],
				joindateIndex:0,
				joinDateList:['今天','昨天','选择日期'],
			},
			loginCallBackType:'',
		}
	},
	onLoad(params){
		if(params.s_id)
		{
			this.s_id = params.s_id;
			
		}
	},
	onShow(){
		if(this.s_id > 0)
		{
			this.getEditInfo();
		}
	},
	onShareAppMessage(){
		var shareInfo = this.shareInfo;
		var title = shareInfo.title?shareInfo.title:'推荐您使用'+this.$api.getSystemConfig('wxMiniProgram.xcx_name');
		if(shareInfo.s_id)
		{
			var path = '/pages/form/statistics/detail?s_id='+this.shareInfo.s_id;
		}else{
			var path = '/pages/index/index';
		}
		var shareObj = {
			title:title,
			path:path
		}
		if(shareInfo.imageUrl)
		{
			shareObj.imageUrl = shareInfo.imageUrl;
		}
		return shareObj;
	},
	methods: {
		async wechatRegister(e){
			if(e.code == 1)
			{
				var that = this;
				switch(that.loginCallBackType)
				{
					case 'buka':
						that.getDetailInfo();
						that.changeUserList(2,'bukaTime');
						break;
					case 'submit':
						await that.getEditInfo(); 
						var index = (typeof that.selUserIndex == 'number')?that.selUserIndex:'';
						that.submit(index);
						break;
				}
				that.selUserIndex = '';
				that.loginCallBackType = '';
			}
		},
		shareFunc(){
			var info = this.info;
			var posterData = {
				title:info.title,
				nickname:info.nickname,
				qrcodeUrl:info.uni_qrcode,
				path:'pages/form/daka/detail?s_id='+info.s_id
			};
			this.$refs.share_wechat.setShowStatus(true,posterData);
		},
		showOpe(index){
			var that = this;
			var info = that.info;
			var data = {
				is_me:info.is_me,
				s_id:info.s_id,
				time_status:info.time_status,
				type:info.type,
				join_count:info.join_count,
			};
			if(info.s_type_info.can_bk == 1)
			{
				data.can_buka = info.s_type_info.can_bk;
			}
			this.$refs.detail_opelist.initData(data,function(res){
				if(res.opeType == 'delete')
				{
					that.$api.navbackTo();
				}
				else if(res.opeType == 'setTime')
				{
					that.getEditInfo();
				}
				else if(res.opeType == 'buka')
				{
					that.changeUserList(2,'bukaTime');
				}
			});
		},
		getEditInfo(){
			var that = this;
			if(that.s_id > 0)
			{
				that.$http.post('statistics/getDetailInfo',{
					s_id:that.s_id
				}).then(res=>{
					if(res.code == 1)
					{
						var info = res.data.info;
						that.info = info;
						if(info.bgPath)
						{
							that.$refs.add_sel_bg.initBgFunc(info.bgPath);
						}
						if(info.content_list)
						{
							let join_contentlist = JSON.parse(info.content_list);
							that.$refs.add_sel_button.initDataList(join_contentlist)
						}
						if(res.data.buttonInfo)
						{
							that.buttonInfo = res.data.buttonInfo;
							that.buttonInfo.show = true;
						}
						that.shareInfo = {
							title:info.title,
							s_id:info.s_id,
							imageUrl:info.bgPath.qnPath
						}
						//that.userListData.list = info.userList;
						//获取参加列表
						that.getJoinList(that.userListData.joindateIndex);
						if(info.join_datalist)// && info.is_me == 1
						{
							//获取参与人员统计
							that.getDetailJoinUserName(that.userListData.dateIndex);
						}
						
					}else{
						that.$api.msg(res.msg);
					}
				})
			}
		},
		changeUserList(index,key){
			if(index == 2)
			{
				var checkLogin = this.$api.checkLogin(false);
				if(checkLogin == false)
				{
					this.loginCallBackType = 'buka';
					this.$refs.wechat_login.saveShowStatus(true);
					return ;
				}
				this.mxDatePicker.key = key;
				this.mxDatePicker.type = 'date'; 
				this.mxDatePicker.showPicker = true;
				var time = this.mxDatePicker[key];
				this.mxDatePicker.value = time;
			}else{
				if(key == 'userListTime')
				{
					if(index == this.userListData.dateIndex)
					{
						return ;
					}
					this.userListData.dateIndex = index;
					this.getDetailJoinUserName(index);
				}else if(key == 'joinListTime')
				{
					if(index == this.userListData.joindateIndex)
					{
						return ;
					}
					this.userListData.joindateIndex = index;
					this.getJoinList(index);
				}
				
			}
		},
		DatePickerEnd(e) {//选择
			this.mxDatePicker.showPicker = false;
			if(e) {
				var key = this.mxDatePicker.key;
				this.mxDatePicker[key] = e.value;
				if(key == 'userListTime')
				{
					this.userListData.dateIndex = 2;
					this.userListData.dateList[2] = e.value;
					this.getDetailJoinUserName(2);
				}else if(key == 'joinListTime'){
					this.userListData.joindateIndex = 2;
					this.userListData.joinDateList[2] = e.value;
					this.getJoinList(2);
				}else if(key == 'bukaTime')
				{
					var that = this;
					var time = e.value;
					that.$api.loadingMsg('');
					that.$http.post('statistics/checkBeforeJoin',{
						time:e.value,
						s_id:that.s_id
					}).then(res=>{
						if(res.code == 1)
						{
							that.submit(-1,time)
						}else{
							that.$api.msg(res.msg);
						}
					})
					
				}
				
			}
		},
		getDetailJoinUserName(timeType){
			var that = this;
			if(timeType == 2)
			{
				var time = this.userListData.dateList[2];
			}else{
				var time = '';
			}
			that.$http.post('statistics/getDetailJoinUserName',{
				timeType,time,s_id:that.s_id
			}).then(res=>{
				if(res.code == 1)
				{
					that.userListData.list = res.data.userList
					that.userListData.successCount = res.data.successCount;
				}else{
					that.userListData.list = [];
					that.userListData.successCount = 0;
				}
			})
		},
		getJoinList(timeType){
			var that = this;
			if(timeType == 2)
			{
				var time = this.userListData.joinDateList[2];
			}else{
				var time = '';
			}
			that.$http.post('statistics/getJoinList',{
				s_id:that.s_id,
				orderby:that.joinListOrder[that.joinListOrderIndex].order,
				timeType,time
			}).then(res=>{
				if(res.code == 1)
				{
					that.joinList = res.data.list;
					that.joinListEmpty = '';
				}else{
					that.joinList = [];
					that.joinListEmpty = res.msg;
				}
			})
		},
		changeOrder(){
			var that = this;
			var joinListOrder = that.joinListOrder;
			let itemList = [];
			joinListOrder.forEach(item=>{
				itemList.push(item.text);
			})
			uni.showActionSheet({
				itemList:itemList,
				success(e){
					var index = e.tapIndex;
					
					if(index.toString() != that.joinListOrderIndex.toString())
					{
						that.joinListOrderIndex = index;
						that.$api.loadingMsg(''); 
						that.getJoinList();
					}
				}
			})
		},
		makePhone(phone){
			this.$api.makePhone(phone);
		},
		joinDetailope(index,type){
			var that = this;
			var info = that.joinList[index];
			switch(type){
				case 'detail':
					this.$api.navigateTo('/pages/form/statistics/join-detail-show?join_id='+info.join_id);
					break;
				case 'save':
					this.$api.navigateTo('/pages/form/statistics/join-detail-edit?join_id='+info.join_id);
					break;	
				case 'del':
					that.$api.showModal('确认删除','删除后不可恢复',function(res){
						if(res == 'yes'){
							that.delJoinInfo(info.join_id,index);
						}
					})
					break;	
			}
		},
		delJoinInfo(join_id,index){
			var that = this;
			that.$api.loadingMsg('正在删除');
			that.$http.post('statistics/joinStatisticsDelete',{
				join_id
			}).then(res=>{
				that.$api.msg(res.msg);
				if(res.code == 1)
				{
					that.getEditInfo();
				}
			});
		},
		
		
		
		
		async submit(index='',join_time=''){
			var that = this;
			//判断是否登录
			var checkLogin = that.$api.checkLogin(false);
			if(checkLogin == false)
			{
				that.loginCallBackType = 'submit';
				that.selUserIndex = index;
				that.$refs.wechat_login.saveShowStatus(true);
				return ;
			}
			if(this.buttonInfo.bottonStatus == false && !join_time)
			{
				return false; //当设置jointime的时候表示为补卡 不进行拦截判断
			}
			index = (typeof index == 'number')?index:-1;
			if(index > -1 && that.info.again_more == -1)
			{
				var selUserInfo = that.userListData.list[index];
				if(selUserInfo && selUserInfo.status == 1)
				{
					that.$api.msg('该人员已填写');
					return ;
				}
			}
			if(that.info.join_datalist == '')
			{
				//直接参与
				that.$api.showModal('确认参加','',async function(res){
					if(res == 'yes')
					{
						var resData = '';
						that.$api.loadingMsg(''); 
						that.$http.post('statistics/joinStatistics',{
							resData,
							s_id:that.info.s_id,
							join_time:join_time
						}).then(res=>{
							that.$api.msg(res.msg)
							if(res.code == 1)
							{
								that.getEditInfo();
							}
						})
					}
				})
			}else{
				that.$api.navigateTo('/pages/form/statistics/join?s_id='+that.s_id+'&default_user_index='+index+'&join_time='+join_time);
			}
			
		}
	}
}